package com.feiyu.admin.controller;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.feiyu.admin.entity.DictGoodsEntity;
import com.feiyu.admin.entity.GoodsEntity;
import com.feiyu.admin.entity.ShopEntity;
import com.feiyu.admin.service.GoodsService;
import com.feiyu.admin.service.ShopService;
import com.feiyu.admin.service.openFeign.ShopsAndGoodsFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.feiyu.admin.entity.CheckGoodsEntity;
import com.feiyu.admin.service.CheckGoodsService;
import com.feiyu.common.utils.PageUtils;
import com.feiyu.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * @author wanghanbin
 * @email 1336891708@qq.com
 * @date 2021-04-08 22:35:19
 */
@RestController
public class CheckGoodsController {
    @Autowired
    private CheckGoodsService checkGoodsService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private ShopsAndGoodsFeignService shopsAndGoodsFeignService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 商品审核订单带参查询
     */
    @PostMapping("/searchCheckGoodsByParam")
    public R searchCheckGoodsByParam(@RequestBody Map<String, Object> params) {
        PageUtils page = checkGoodsService.queryPage(params);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("totalCount", page.getTotalCount());
        hashMap.put("pageSize", page.getPageSize());
        hashMap.put("totalPage", page.getTotalPage());
        hashMap.put("currPage", page.getCurrPage());
        List<?> pageList = page.getList();
        List<Map<String, Object>> checkGoodInfo_list = new ArrayList<>();
        for (int i = 0; i < pageList.size(); i++) {
            HashMap<String, Object> checkGoodInfoImages = new HashMap<>();
            HashMap<String, Object> checkGoodsInfo = new HashMap<>();
            CheckGoodsEntity checkGoodsEntity = (CheckGoodsEntity) pageList.get(i);
            String checkGoodsImages = checkGoodsEntity.getGoodsImages();
            String[] split = checkGoodsImages.split(";");
            String url = "https://test-wanghanbin.oss-cn-hangzhou.aliyuncs.com/feiyuImages/";
            for (int j = 0; j < split.length; j++) {
                checkGoodInfoImages.put("goodImage_" + (j + 1), url + split[j]);
            }
            Integer tagId = checkGoodsEntity.getGoodsTagId();
            DictGoodsEntity dictGoodsEntity = new DictGoodsEntity();
            dictGoodsEntity.setDictGoodsId(tagId);
            R goodsTag = shopsAndGoodsFeignService.getGoodsTagNameByTagId(dictGoodsEntity);
            Map<String, Object> data = (Map<String, Object>) goodsTag.get("data");
            Integer shopId = checkGoodsEntity.getShopId();
            ShopEntity shopEntity = shopService.getById(shopId);
            checkGoodsInfo.put("checkGoodsId", checkGoodsEntity.getCheckGoodsId());
            checkGoodsInfo.put("goodsId", checkGoodsEntity.getGoodsId());
            checkGoodsInfo.put("goodsName", checkGoodsEntity.getGoodsName());
            checkGoodsInfo.put("goodsIntro", checkGoodsEntity.getGoodsIntro());
            checkGoodsInfo.put("shopId", checkGoodsEntity.getShopId());
            checkGoodsInfo.put("shopName", shopEntity.getShopName());
            checkGoodsInfo.put("goodsImages", checkGoodInfoImages);
            checkGoodsInfo.put("goodsLicense", checkGoodsEntity.getGoodsLicense());
            checkGoodsInfo.put("goodsRent", checkGoodsEntity.getGoodsRent());
            checkGoodsInfo.put("goodsDeposit", checkGoodsEntity.getGoodsDeposit());
            checkGoodsInfo.put("goodsPrice", checkGoodsEntity.getGoodsPrice());
            checkGoodsInfo.put("goodsTagId", checkGoodsEntity.getGoodsTagId());
            checkGoodsInfo.put("goodsTagName", data.get("dictGoodsLable"));
            checkGoodsInfo.put("goodsTimeMode", checkGoodsEntity.getGoodsTimeMode());
            checkGoodsInfo.put("isAllowedBuy", checkGoodsEntity.getIsAllowedBuy());
            checkGoodsInfo.put("goodsPostageMode", checkGoodsEntity.getGoodsPostageMode());
            checkGoodsInfo.put("goodsMaxRenewalTime", checkGoodsEntity.getGoodsMaxRenewalTime());
            checkGoodsInfo.put("goodsNums", checkGoodsEntity.getGoodsNums());
            checkGoodsInfo.put("checkGoodsApplyTime", checkGoodsEntity.getCheckGoodsApplyTime());
            checkGoodsInfo.put("checkState", checkGoodsEntity.getCheckState());
            checkGoodsInfo.put("checkUserId", checkGoodsEntity.getCheckUserId());
            checkGoodsInfo.put("checkTime", checkGoodsEntity.getCheckTime());
            checkGoodsInfo.put("checkRe", checkGoodsEntity.getCheckRe());
            checkGoodInfo_list.add(checkGoodsInfo);
        }
        hashMap.put("list", checkGoodInfo_list);
        return R.ok("成功").put("data", hashMap);
    }


    /**
     * 商品审核订单精确查询
     */
    @PostMapping("/searchCheckGoodsById")
    public R searchCheckGoodsById(@RequestBody CheckGoodsEntity checkGoodsEntity) {
        CheckGoodsEntity entity = checkGoodsService.getById(checkGoodsEntity.getCheckGoodsId());
        HashMap<String, Object> checkGoodInfoImages = new HashMap<>();
        HashMap<String, Object> checkGoodsInfo = new HashMap<>();
        String checkGoodsImages = entity.getGoodsImages();
        String[] split = checkGoodsImages.split(";");
        String url = "https://test-wanghanbin.oss-cn-hangzhou.aliyuncs.com/feiyuImages/";
        for (int j = 0; j < split.length; j++) {
            checkGoodInfoImages.put("goodImage_" + (j + 1), url + split[j]);
        }
        Integer tagId = entity.getGoodsTagId();
        DictGoodsEntity dictGoodsEntity = new DictGoodsEntity();
        dictGoodsEntity.setDictGoodsId(tagId);
        R goodsTag = shopsAndGoodsFeignService.getGoodsTagNameByTagId(dictGoodsEntity);
        Map<String, Object> data = (Map<String, Object>) goodsTag.get("data");
        Integer shopId = entity.getShopId();
        ShopEntity shopEntity = shopService.getById(shopId);
        checkGoodsInfo.put("checkGoodsId", entity.getCheckGoodsId());
        checkGoodsInfo.put("goodsId", entity.getGoodsId());
        checkGoodsInfo.put("goodsName", entity.getGoodsName());
        checkGoodsInfo.put("goodsIntro", entity.getGoodsIntro());
        checkGoodsInfo.put("shopId", entity.getShopId());
        checkGoodsInfo.put("shopName", shopEntity.getShopName());
        checkGoodsInfo.put("goodsImages", checkGoodInfoImages);
        checkGoodsInfo.put("goodsLicense", entity.getGoodsLicense());
        checkGoodsInfo.put("goodsRent", entity.getGoodsRent());
        checkGoodsInfo.put("goodsDeposit", entity.getGoodsDeposit());
        checkGoodsInfo.put("goodsPrice", entity.getGoodsPrice());
        checkGoodsInfo.put("goodsTagId", entity.getGoodsTagId());
        checkGoodsInfo.put("goodsTagName", data.get("dictGoodsLable"));
        checkGoodsInfo.put("goodsTimeMode", entity.getGoodsTimeMode());
        checkGoodsInfo.put("isAllowedBuy", entity.getIsAllowedBuy());
        checkGoodsInfo.put("goodsPostageMode", entity.getGoodsPostageMode());
        checkGoodsInfo.put("goodsMaxRenewalTime", entity.getGoodsMaxRenewalTime());
        checkGoodsInfo.put("goodsNums", entity.getGoodsNums());
        checkGoodsInfo.put("checkGoodsApplyTime", entity.getCheckGoodsApplyTime());
        checkGoodsInfo.put("checkState", entity.getCheckState());
        checkGoodsInfo.put("checkUserId", entity.getCheckUserId());
        checkGoodsInfo.put("checkTime", entity.getCheckTime());
        checkGoodsInfo.put("checkRe", entity.getCheckRe());
        return R.ok("成功").put("data", checkGoodsInfo);
    }

    /**
     * 商品审核订单审核处理
     */
    @PostMapping("/checkGoods")
    public R checkGoods(@RequestBody CheckGoodsEntity checkGoods,
                        HttpServletRequest request) {
        String userId = request.getHeader("userId");
        checkGoods.setCheckUserId(Integer.parseInt(userId));
        checkGoodsService.updateById(checkGoods);
        if (checkGoods.getCheckState() == 1) {
            CheckGoodsEntity entity = checkGoodsService.getById(checkGoods.getCheckGoodsId());
            GoodsEntity goodsEntity = new GoodsEntity();
            goodsEntity.setGoodsId(entity.getGoodsId());
            goodsEntity.setGoodsName(entity.getGoodsName());
            goodsEntity.setGoodsIntro(entity.getGoodsIntro());
            goodsEntity.setGoodsImages(entity.getGoodsImages());
            goodsEntity.setGoodsLicense(entity.getGoodsLicense());
            goodsEntity.setGoodsRent(entity.getGoodsRent());
            goodsEntity.setGoodsDeposit(entity.getGoodsDeposit());
            goodsEntity.setGoodsPrice(entity.getGoodsPrice());
            goodsEntity.setGoodsTagId(entity.getGoodsTagId());
            goodsEntity.setGoodsTimeMode(entity.getGoodsTimeMode());
            goodsEntity.setIsAllowedBuy(entity.getIsAllowedBuy());
            goodsEntity.setGoodsPostageMode(entity.getGoodsPostageMode());
            goodsEntity.setGoodsMaxRenewalTime(entity.getGoodsMaxRenewalTime());
            goodsEntity.setGoodsNums(entity.getGoodsNums());
            goodsService.updateById(goodsEntity);
        }
        return R.ok("成功").put("data", null);
    }

}
